//
//  HomeViewController.swift
//  WB
//
//  Created by KenLiang on 16/4/5.
//  Copyright © 2016年 KenLiang. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {
    
    // MARK:- 自定义属性
    var isPresented : Bool = false
    
    // MARK:- 懒加载属性
    private lazy var titleBtn : TitleButton = TitleButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        visitorView.addRotation()
        if !isLogin {
            return
        }
        
        setupNavBarItem()
    }

}

// MARK:-设置首页navBar的左右按钮和中间按钮
extension HomeViewController {
    private func setupNavBarItem() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
        
        titleBtn.setTitle("MJCoder", forState: .Normal)
        titleBtn.addTarget(self, action: "titleViewBtnClick:", forControlEvents: .TouchUpInside)
        navigationItem.titleView = titleBtn
    }
}

// MARK:- 监听中间按钮点击
extension HomeViewController {
    @objc private func titleViewBtnClick(titleButton : TitleButton) {
        titleButton.selected = !titleButton.selected
        
        //创建弹出控制器
        let popoverVc = PopoverViewController()
        
        //设置弹出样式
        popoverVc.modalPresentationStyle = .Custom
        
        //设置转场代理
        popoverVc.transitioningDelegate = self
        
        //modal出控制器
        presentViewController(popoverVc, animated: true, completion: nil)
    }
}


// MARK:- 代理方法
extension HomeViewController : UIViewControllerTransitioningDelegate {
    //改变弹出View尺寸
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        return LMJPresentationController(presentedViewController : presented, presentingViewController:  presenting)
    }
    
    //自定义弹出View动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    
    //自定义消失View动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
    
}

// MARK:- 弹出和消失动画代理
extension HomeViewController : UIViewControllerAnimatedTransitioning {
    //动画执行时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    //获取转成上下文,从而获取消失的view和弹出的view
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(transitionContext) : animationForDismissView(transitionContext)
    }
    
    
    // MARK:- 自定义弹出VIew动画
    private func animationForPresentedView(transitionContext:UIViewControllerContextTransitioning) {
        //获取弹出View
        let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //将弹出的View添加到containView中
        transitionContext.containerView()?.addSubview(presentedView)
        
        //执行动画
        presentedView.transform = CGAffineTransformMakeScale(1.0, 0)
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)

        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            presentedView.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                //必须告知转成上下文已经完成了动画
                transitionContext.completeTransition(true)
        }
    }
    
    
    // MARK:- 自定义消失View动画
    private func animationForDismissView(transitionContext:UIViewControllerContextTransitioning) {
        
        //获取消失的View
        let dismissedView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            dismissedView.transform = CGAffineTransformMakeScale(1.0, 0.0000001)
            }) { (_) -> Void in
                dismissedView.removeFromSuperview()
                //告知转成上下文已经完成了动画
                transitionContext.completeTransition(true)
        }
        
    }
}
